package org.deva.erp.base.util;

import java.text.MessageFormat;
import java.util.Map;
import java.util.regex.PatternSyntaxException;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.ValidatorException;

/**
 * Project: erp<br>
 * Description: 统一验证器<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 * 
 * attribute：req_trim		去除空格
 * attribute：req_notnull	必须
 * attribute：max_length		最大
 * attribute：min_length		最小
 * attribute：self_regular	正则
 * attribute：selfregularmsg		正则表达式信息，配套regular使用
 * attribute：sysregular		系统内置正则
 */
@FacesValidator(value = "validator")
public class ERPValidator implements javax.faces.validator.Validator {
	private static final String REQTRIM = "req_trim";
	private static final String REQUIRED = "req_notnull";
	private static final String MAXLENGTH = "max_length";
	private static final String MINLENGTH = "min_length";
	private static final String REGULAR = "self_regular";
	private static final String REGULARMSG = "self_regularmsg";
	private static final String SYSREGULAR = "sys_regular";
	
	private static final String LABEL = "label";
	
	private static final String REQUIREDMSG = "required_msg";
	private static final String MINLENGTHMSG = "minlength_msg";
	private static final String MAXLENGTHMSG = "maxlength_msg";
	private static final String SYSREGULARMSG = "regular_msg";
	private static final String SYSREGULARINVALIDMSG = "regular_invalid_msg";
	private static final String SYSREGULARNOTEXIST = "regular_notexist";
	
	@Override
	public void validate(FacesContext context, UIComponent component, Object object) throws ValidatorException {
		Map<String, Object> attrs = component.getAttributes();
		String label = attrs.get(LABEL) == null ? component.getId() : attrs.get(LABEL).toString();
		String value = object == null ? "" : (attrs.get(REQTRIM) != null && Boolean.valueOf(String.valueOf(attrs.get(REQTRIM))) ? String.valueOf(object).replaceAll("\\s*", "") : String.valueOf(object));
		// 验证非空
		if(Boolean.valueOf(String.valueOf(attrs.get(REQUIRED))) && "".equals(value)) {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, AppAttributes.resValid.getString(REQUIREDMSG)));
		}
		// 验证最小长度
		Object min = attrs.get(MINLENGTH);
		if(min != null && value.length() < Integer.valueOf(String.valueOf(min))) {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, 
					MessageFormat.format(AppAttributes.resValid.getString(MINLENGTHMSG), String.valueOf(min))));
		}
		// 验证最大长度
		Object max = attrs.get(MAXLENGTH);
		if(max != null && value.length() > Integer.valueOf(String.valueOf(max))) {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, 
					MessageFormat.format(AppAttributes.resValid.getString(MAXLENGTHMSG), String.valueOf(max))));
		}
		// 自定义正则表达式
		Object regular = attrs.get(REGULAR);
		try {
			if(regular != null && !"".equals(value) && !value.matches(String.valueOf(regular))) {
				throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, 
						attrs.get(REGULARMSG) == null ? MessageFormat.format(AppAttributes.resValid.getString(SYSREGULARMSG), regular) :
							String.valueOf(attrs.get(REGULARMSG))));
			}
		} catch (PatternSyntaxException e) {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, 
					MessageFormat.format(AppAttributes.resValid.getString(SYSREGULARINVALIDMSG), regular)));
		}
		// 内置正则
		Object sysregular = attrs.get(SYSREGULAR);
		if(sysregular != null) {
			String reg;
			try {
				reg = AppAttributes.resValid.getString(String.valueOf(sysregular));
			} catch(java.util.MissingResourceException e) {
				throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, 
						MessageFormat.format(AppAttributes.resValid.getString(SYSREGULARNOTEXIST), sysregular)));
			}
			try {
				if(!"".equals(value) && !value.equals(reg)) {
					throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, 
							AppAttributes.resValid.getString(sysregular + "_msg")));
				}
			} catch (PatternSyntaxException e) {
				throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, label, 
						MessageFormat.format(AppAttributes.resValid.getString(SYSREGULARINVALIDMSG), reg)));
			}
		}
	}
}
